package com.alibaba.org.controller;


import com.alibaba.basic.jwt.UserInfo;
import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.LoginContext;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.org.domain.Department;
import com.alibaba.org.domain.Systemdictionarydetail;
import com.alibaba.org.query.DepartmentQuery;
import com.alibaba.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**d
     * restful 风格 查询所有
     *
     * @return
     */
    @GetMapping
    public List<Department> findAdd(){
        return departmentService.findAll();
    }

    /**
     * 根据id查询一个
     * 如果我们接口只有一个参数的时候 我们可以使用 路径变量的形式去接收参数 多个参数的话使用实体去接收
     * 我们原来请求：http://localhost:8080/dept/findById?id=1
     * 路径变量：http://localhost:8080/dept/1
     */
      @GetMapping("/{id}")
      @ApiOperation(value = "通过ID查询" )
      public Department findById(@ApiParam(value="部门ID",required = true)  @PathVariable("id") Long id) {
          return departmentService.finById(id);
      }

    /**
     * 添加和修改
     * 前后端分离之后 前后端交互数据全部都是json数据 本质是个字符串
     * @RequestBody 来将json数据转成我们的实体 但是我们的参数中只能有一个实体 不能有多个
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null == department.getId()){
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
//    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();
        return departmentService.queryByPage(query);
    }



    /**
     * 数据字典
     */
    @GetMapping("/detail/{id}")
    public List<Systemdictionarydetail> detailById(@PathVariable("id") Long id) {
        return departmentService.detailById(id);
    }

    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult deleteId(@RequestBody List<Long> ids) {
        departmentService.deleteId(ids);
        return AjaxResult.me().success();
    }

    /**
     * 获取部门树
     */
    @GetMapping("/tree")
//    @PreAuthorize(name = "部门树",sn = "department:tree")
    public List<Department> tree() {
        return departmentService.tree();
    }



}
